package com.example.tab02;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.example.fragment.AddFragment;
import com.example.fragment.FrdFragment;
import com.example.fragment.SettingFragment;
import com.example.fragment.WeiXinFragment;

public class MainActivity extends Activity implements OnClickListener{

	public LinearLayout weixin_tab;
	public LinearLayout frd_tab;
	public LinearLayout add_tab;
	public LinearLayout setting_tab;
	
	public ImageButton weixin_img;
	public ImageButton frd_img;
	public ImageButton add_img;
	public ImageButton setting_img;
	
	public WeiXinFragment weixin_fragment;
	public FrdFragment frd_fragment;
	public AddFragment add_fragment;
	public SettingFragment setting_fragment;
	//public DetailsActivity detailsActivity;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);


		intViews();
		intEvents();
		setContent(0);
	}



	private void intViews() {
		weixin_tab=(LinearLayout) findViewById(R.id.weixin_tab);
		frd_tab=(LinearLayout) findViewById(R.id.frd_tab);
		add_tab=(LinearLayout) findViewById(R.id.add_tab);
		setting_tab=(LinearLayout) findViewById(R.id.setting_tab);
		
		weixin_img=(ImageButton) findViewById(R.id.weixin_img);
		frd_img=(ImageButton) findViewById(R.id.frd_img);
		add_img=(ImageButton) findViewById(R.id.add_img);
		setting_img=(ImageButton) findViewById(R.id.setting_img);
		
		weixin_fragment=new WeiXinFragment();
		frd_fragment=new FrdFragment();
		add_fragment=new AddFragment();
		setting_fragment=new SettingFragment();
		//detailsActivity=new DetailsActivity();
	}

	private void intEvents() {
		weixin_tab.setOnClickListener(this);
		frd_tab.setOnClickListener(this);
		add_tab.setOnClickListener(this);
		setting_tab.setOnClickListener(this);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onClick(View v) {
		resetImage();
		switch(v.getId()){
		case R.id.weixin_tab:			
			 setContent(0);
			 break;
		case R.id.frd_tab:
			 setContent(1);
			 break;
		case R.id.add_tab:
			 setContent(2);
			 break;
		case R.id.setting_tab:
			 setContent(3);
			 break;
		default:
            break;
		}
		
	}

	private void setContent(int item) {
		
		FragmentManager fm=getFragmentManager();
		FragmentTransaction transaction=fm.beginTransaction();
		switch(item){
		case 0:
			transaction.replace(R.id.content,weixin_fragment);
			weixin_img.setImageResource(R.drawable.tab_weixin_pressed);
			break;
		case 1:
			transaction.replace(R.id.content,frd_fragment);
			frd_img.setImageResource(R.drawable.tab_find_frd_pressed);
			break;
		case 2:
			transaction.replace(R.id.content,add_fragment);
			add_img.setImageResource(R.drawable.tab_address_pressed);
			break;
		case 3:
			transaction.replace(R.id.content,setting_fragment);
			setting_img.setImageResource(R.drawable.tab_settings_pressed);
			break;
		default:
            break;
		}
		
		transaction.commit();
	}

	private void resetImage() {
		weixin_img.setImageResource(R.drawable.tab_weixin_normal);
		frd_img.setImageResource(R.drawable.tab_find_frd_normal);	
		add_img.setImageResource(R.drawable.tab_address_normal);
		setting_img.setImageResource(R.drawable.tab_settings_normal);
		
	}
}
